﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager : MonoBehaviour {
    public static UIManager Instance;
    public Transform PanelRoot;

    private Stack<BasePanel> panels = new Stack<BasePanel>();
    void Awake()
    {
        Instance = this;
    }
    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    public void Push(string panelName)
    {
        Transform transform = PanelRoot.Find(panelName);
        if(transform != null)
        {
            transform.SetAsLastSibling();
            transform.gameObject.SetActive(true);
            BasePanel basePanel = transform.GetComponent<BasePanel>();
            panels.Push(basePanel);
            basePanel.OnActive();
        }
    }

    public void Pop()
    {
        BasePanel basePanel = panels.Pop();
        if(basePanel != null)
        {
            basePanel.gameObject.SetActive(false);
        }
        if(panels.Count != 0)
        {
            basePanel = panels.Peek();
            basePanel.OnActive();
        }
    }

    public void Open(string panelName)
    {
        foreach(BasePanel basePanel in panels)
        {
            basePanel.gameObject.SetActive(false);
        }
        panels.Clear();
        Push(panelName);
    }
}
